﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;

namespace KouSuan.Models
{
    public class ExpressionModel : ViewModelBase
    {
        private int _TabIndex;

        public int TabIndex
        {
            get { return _TabIndex; }
            set
            {
                if (value != _TabIndex)
                {
                    _TabIndex = value;
                    RaisePropertyChanged(() => TabIndex);
                }
            }
        }


        private int _LeftNum;

        public int LeftNum
        {
            get { return _LeftNum; }
            set
            {
                if (value != _LeftNum)
                {
                    _LeftNum = value;
                    RaisePropertyChanged(() => LeftNum);
                }
            }
        }

        private int _RightNum;

        public int RightNum
        {
            get { return _RightNum; }
            set
            {
                if (value != _RightNum)
                {
                    _RightNum = value;
                    RaisePropertyChanged(() => RightNum);
                }
            }
        }

        private char _Oprator;

        public char Oprator
        {
            get { return _Oprator; }
            set
            {
                if (value != _Oprator)
                {
                    _Oprator = value;
                    RaisePropertyChanged(() => Oprator);
                }
            }
        }

        private string _UserAnswer;

        /// <summary>
        /// 用户输入的答案
        /// </summary>
        public string UserAnswer
        {
            get { return _UserAnswer; }
            set
            {
                if (value != _UserAnswer)
                {
                    _UserAnswer = value;
                    RaisePropertyChanged(() => UserAnswer);
                }
            }
        }

        /// <summary>
        /// 获取真正的答案
        /// </summary>
        public int Answer
        {
            get
            {
                int result;

                switch (Oprator)
                {
                    case '+':
                        result = LeftNum + RightNum;
                        break;
                    case '-':
                        result = LeftNum - RightNum;
                        break;
                    case '×':
                        result = LeftNum * RightNum;
                        break;
                    case '÷':
                        result = LeftNum / RightNum;
                        break;
                    default:
                        result = 0;
                        break;
                }

                return result;
            }
        }

        private bool _IsRight;

        /// <summary>
        /// 
        /// </summary>
        public bool IsRight
        {
            get
            {
                return _IsRight;
            }
        }

        public string Expression
        {
            get
            {
                return this.ToString();
            }
        }

        public void Check()
        {
            bool _isright = _IsRight;

            int uanswer = 0;
            if (int.TryParse(UserAnswer, out uanswer))
            {
                _IsRight = uanswer == Answer;
            }
            else
            {
                _IsRight = false;
            }

            if (_isright != _IsRight)
            {
                RaisePropertyChanged(() => IsRight);
            }
        }

        public override string ToString()
        {
            return string.Format("{0}{1}{2}", this.LeftNum, this.Oprator, this.RightNum);
        }
    }
}
